<?php
/**
 * 管理员管理知识点
 */

class Admin_test_point extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in')) {
            redirect('home');
        }
        //权限检测
        if ($this->session->userdata('r_role_id') != 1) {
            redirect('home');
        }
    }

    /**
     * 考点管理的主页面
     * @return [type] [description]
     */
    public function index()
    {
        $data['title']='考点管理';
        
        $this->load->model('test_point_model');
        $data['test_points']=$this->test_point_model->get_all();

        $this->load->view('admin_test_point/index', $data);
    }


    /**
     * 添加考点
     *
     */
    public function add()
    {
        //表单验证
        if ($this->form_validation->run('admin_test_point/add')) {
            //post方法，获取数据
            $content=$this->input->post('content');
            $module_ids=$this->input->post('relative_modules');
            $knowledge_point_number = $this->input->post('knowledge_point_number');
            if (is_numeric($knowledge_point_number)) {
                $knowledge_point_number=intval($knowledge_point_number);
            }
//            else break;

            $item = array('content' => $content );
            //加载模型
            $this->load->model('test_point_model');
            $this->load->model('knowledge_point_model');
            if ($this->test_point_model->add($item)) {
                $test_point_id=$this->db->insert_id();

                //将添加的知识点写入数据库
                for ($i=1; $i <= $knowledge_point_number; $i++) {
                    $content=$this->input->post('knowledge_point_input_'.$i);
                    if ($content) {
                        //如果有内容才写入
                        $item = array(
                            'content' => $content,
                            'test_point_id' => $test_point_id
                            );
                        if (!$this->knowledge_point_model->add($item)) {
                            $this->session->set_flashdata('message', '添加知识点失败');
                            redirect('admin_test_point/index');
                        }
                    }
                }
                //添加和考点相关的模块
                if ($this->test_point_model->add_relative_modules($test_point_id, $module_ids)) {
                    $this->session->set_flashdata('message', '操作成功');
                    redirect('admin_test_point/index');
                }
            }
            $this->session->set_flashdata('message', '操作失败');
            redirect('admin_test_point/add');
        }

        //get方法
        $data['title']='添加考点';
        $this->load->model('module_model');
        $data['modules']=$this->module_model->get_all();

        $this->load->view('admin_test_point/add', $data);
    }

    /**
     * 删除一个考点
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        //加载模型
        $this->load->model('test_point_model');
        if ($this->test_point_model->delete_cascade($id)) {
            $this->session->set_flashdata('message', '操作成功');
        } else {
            $this->session->set_flashdata('message', '操作失败,存在关于这个考点的引用(模块或考题)');
        }
        redirect('admin_test_point/index');
    }

    /**
     * 编辑一个考点
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function edit($id=0)
    {
        if ($id) {
            //get方法
            
            $this->load->model('test_point_model');
            $test_point=$this->test_point_model->get_by_id($id);
            if (!$test_point) {
                return $this->load->view('error');
            }
            $data['test_point']=$test_point;
            //加载模型
            $this->load->model('module_model');
            $this->load->model('knowledge_point_model');

            $data['modules']=$this->module_model->get_all_with_big_lecture_name();
            $data['knowledge_points']=$this->knowledge_point_model->get_all();
            $data['relative_knowledge_points']=$this->test_point_model->get_relative_knowledge_points($test_point['id']);
            $data['relative_modules']=$this->test_point_model->get_relative_modules($test_point['id']);
            $data['test_point_id']=$test_point['id'];
            $data['title']='编辑考点';
            $this->load->view('admin_test_point/edit', $data);
        } else {
            //post方法
            $this->load->model('test_point_model');
            $test_point_id=$this->input->post('test_point_id');
            $test_point=$this->test_point_model->get_by_id($test_point_id);
            if (!$test_point) {
                return $this->load->view('error');
            }
            if ($this->form_validation->run('admin_test_point/edit')) {
                //post
                $content=$this->input->post('content');
                $module_ids=$this->input->post('relative_modules');
                $knowledge_point_number = $this->input->post('knowledge_point_number');
                if (is_numeric($knowledge_point_number)) {
                    $knowledge_point_number=intval($knowledge_point_number);
                }
//                else break;


                //加载模型
                $this->load->model('test_point_model');
                $this->load->model('knowledge_point_model');
                

                $item = array('content' => $content);
                //修改相关模块
                if ($this->test_point_model->delete_relative_modules($test_point_id)&&
                    $this->test_point_model->add_relative_modules($test_point_id, $module_ids)) {
                    //修改相关知识点（更新旧的知识点+增加新的知识点）
                    for ($i=1;$i<=$knowledge_point_number;$i++) {
                        $content=$this->input->post('knowledge_point_input_'.$i);
                        if ($content) {
                            //有内容时才进行操作
                            $knowledge_point_id=$this->input->post('knowledge_point_id_input_'.$i);
                            if ($knowledge_point_id) {
                                //如果id存在，做更新操作
                                $current_knowledge_point= array('content' => $content );
                                $this->knowledge_point_model->update($knowledge_point_id, $current_knowledge_point);
                            } else {
                                //id不存在，做插入操作
                                $current_knowledge_point= array(
                                    'content' => $content ,
                                    'test_point_id'=> $test_point_id
                                    );
                                $this->knowledge_point_model->add($current_knowledge_point);
                            }
                        } else {
                            //内容已经被删除，做删除操作
                            $knowledge_point_id=$this->input->post('knowledge_point_id_input_'.$i);
                            if ($knowledge_point_id) {
                                //如果能够获取id，做删除操作
                                if (!$this->knowledge_point_model->delete($knowledge_point_id)) {
                                    $this->session->set_flashdata('message', '删除知识点失败，存在引用这个知识点的题目');
                                    redirect('admin_test_point/index');
                                }
                            }
                        }
                    }
                }
                
                //修改考点内容
                if ($this->test_point_model->update($test_point_id, $item)) {
                    $this->session->set_flashdata('message', '操作成功');
                    redirect('admin_test_point/index');
                } else {
                    $this->session->set_flashdata('message', '操作失败');
                }
            } else {
                //验证失败
                //get
                $data['test_point']=$test_point;
                //加载模型
                $this->load->model('module_model');
                $this->load->model('knowledge_point_model');

                $data['modules']=$this->module_model->get_all();
                $data['knowledge_points']=$this->knowledge_point_model->get_all();
                $data['relative_knowledge_points']=$this->test_point_model->get_relative_knowledge_points($test_point['id']);
                $data['relative_modules']=$this->test_point_model->get_relative_modules($test_point['id']);
                $data['test_point_id']=$test_point['id'];
                $data['title']='编辑考点';
                $this->load->view('admin_test_point/edit', $data);
            }
        }
    }

    /**
     * 查看一个考点的内容
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function check($id=0)
    {
        $this->load->model('test_point_model');
        $test_point=$this->test_point_model->get_by_id($id);
        if (!$test_point) {
            return $this->load->view('error');
        }

        $data['title']='查看考点';
        $data['test_point']=$test_point;
        $data['relative_modules']=$this->test_point_model->get_relative_modules($test_point['id']);
        $data['relative_knowledge_points']=$this->test_point_model->get_relative_knowledge_points($test_point['id']);

        $this->load->view('admin_test_point/check', $data);
    }

    /**
     * ajax方法，通过考点查询相关知识点
     * @return [type] [description]
     */
    public function find_relative_knowledge_points($id)
    {
        $this->load->model('test_point_model');
        $data['knowledge_points']=$this->test_point_model->get_relative_knowledge_points($id);
        $this->load->view('admin_test_point/relative_knowledge_points', $data);
    }
}
